﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
using NetTaste;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using SqlSugar.Extensions;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing.Printing;
using System.Linq.Expressions;
using System.Text;
using VM.Common.DataUntil;
using VM.Common.Encryption5Until;
using VM.Common.Request;
using VM.Common.Result;
using VM.DAL;
using VM.Entity.EntityDto;
using VM.Entity.EntityMap;
using VM.Entity.EntityMap.OAuth;
using VM.Entity.EntityMap.Region;
using VM.Entity.EntityMap.Token;
using VM.Entity.EntityMap.User;
using VM.IBLL.Region;
using VM.IDAL;
using static Dm.net.buffer.ByteArrayBuffer;

namespace VM.BLL.Region
{
    public class RegionBLL : BaseBLL, IRegionBLL
    {

        private readonly ISqlSuggarDAL _ISqlSuggarDAL;
        private static string StaticKey = "VeyMin0924,.!";
        private static double LongTimes = 7 * 24 * 60; //以分钟为计量单位
        private static int OrgId = 42;

        private readonly IConfiguration _IConfiguration;


        public RegionBLL(ISqlSugarClient client, ISqlSuggarDAL sqlSuggarDAL, IConfiguration configuration)
        {
            _ISqlSuggarDAL = sqlSuggarDAL;
            _IConfiguration = configuration;
        }




        /// <summary>
        /// 生成密钥  密钥组成  常量+时间+随机变量+用户名（手机号）+clientId
        /// </summary>
        /// <returns></returns>
        public string CreateKey(int RandomKey, string CreateDate, string UserId, int ClientId)
        {
            string Str = "";
            Str += CreateDate + StaticKey + RandomKey.ToString() + ClientId.ToString();
            return MD5ED.MD5Encrypt32(Str);
        }

        /// <summary>
        /// 根据GUID获取用户信息
        /// </summary>
        /// <param name="Guid"></param>
        /// <returns></returns>
        public DataTable GetTokenByGuid(string Guid, string WXOpneId)
        {

            string GetSql = $@"   SELECT M.Id, A.UserId, A.ClientId, A.Token, A.CreateDate,  A.LongTimes,A.Des, M.UserName ,'{WXOpneId}' WXOpneId
        FROM   dbo.VM_S_Token A
        LEFT JOIN dbo.VM_S_User M ON A.UserId = M.Id
        LEFT JOIN dbo.VM_S_Organization ORG ON M.OrgId=ORG.Id
        LEFT JOIN dbo.VM_S_Emp EMP ON M.EmpId=EMP.Id 
        LEFT JOIN dbo.VM_S_Dept Dept ON m.DeptId=Dept.Id
        LEFT JOIN dbo.VM_S_Position post ON emp.RoleId=post.Id
        WHERE A.GUID = '{Guid}'";

            DataTable dtRes = _ISqlSuggarDAL.ExcuteQuery<DataTable>(GetSql).ToDataTable();

            return dtRes;
        }

        public ApiDataResult<List<sys_region>> getRegionCodeByName(string Province, string City, string County)
        {
            List<sys_region> sysRegions = new List<sys_region>();
            Expression<Func<sys_region, bool>> ProvinceFuncWhere = x => x.name == Province;
            List<sys_region> ProvinceList = _ISqlSuggarDAL.Query(ProvinceFuncWhere).ToList();


            Expression<Func<sys_region, bool>> CityFuncWhere = x => x.name == City;
            List<sys_region> CityList = _ISqlSuggarDAL.Query(CityFuncWhere).ToList();


            Expression<Func<sys_region, bool>> CountyFuncWhere = x => x.name == County;
            List<sys_region> CountyList = _ISqlSuggarDAL.Query(CountyFuncWhere).ToList();

            List<sys_region> listRes = new List<sys_region>();
            ApiDataResult<List<sys_region>> res = new ApiDataResult<List<sys_region>>();
            if (ProvinceList.Count > 0 && CityList.Count > 0 && CountyList.Count > 0)
            {
                sys_region ProvinceC = ProvinceList[0];
                sys_region CityC = CityList[0];
                sys_region CountyC = CountyList[0];
                listRes.Add(ProvinceC);
                listRes.Add(CityC);
                listRes.Add(CountyC);

                //ApiDataResult<sys_region> res = new ApiDataResult<sys_region>();

                res.code = 200;
                res.msg = "查询成功";
                res.data = listRes;
            }
            else
            {
                res.code = 403;
                res.msg = "查询失败，传入省市县名称错误或没找到";
                res.data = null;

            }
            return res;



        }


    }
}